package com.example.testoink;

import java.util.Timer;
import java.util.TimerTask;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

public class Pig {
	
	boolean hungry;
	RelativeLayout workingLayout;
	MainActivity workingActivity;
	int pigId;
	ImageView img1;
	Timer  pigTimer;
	Timer  pigTimer2;
	public Pig(RelativeLayout layout,MainActivity activity,int type){
		
		workingLayout = layout;
		workingActivity = activity;
		hungry = true;
	    
		
	}
	
	public void endAll(){
		
		pigTimer.cancel();
		pigTimer2.cancel();
		
	}
	
	public void eatGrass(){
		
		
		System.out.println(pigId);
		
		if((workingActivity.eatGrassAt(this,pigId))==true){
    		workingActivity.updateCoinBy(50);
    		
    		//dropCoin();  
    		}
	}
	
public void dropCoin(){
	
	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1920/12,1028/8);
		final ImageView coin  = new ImageView(workingActivity);
		coin.setImageResource( R.drawable.coin );
		coin.setX(img1.getX());
		coin.setY(img1.getY()+100);
		workingLayout.addView(coin, params);
		TranslateAnimation tAnimation ;
		 tAnimation = new TranslateAnimation(0,0,0,100); 
  		 tAnimation.setDuration(200);
  		 
  		 tAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				
				 workingLayout.removeView(coin); 
				
			}
		});
  		 
 		// coin.setScaleType(ScaleType.MATRIX);   //required
      coin.setAnimation(tAnimation);
	
}	
	
public void pigStartToDropCoin(){
	
		 pigTimer = new Timer();	

		pigTimer.scheduleAtFixedRate(new TimerTask() {
	
			public void run() {
	            workingActivity.runOnUiThread(new Runnable() //run on ui thread
	             {

	              public void run() 
	              {
	            	
	            	eatGrass();
	              }
	             });
	        }
	    
			},5000,5000);
		
	}

public void tranfrom(){
	
	pigTimer.cancel();
	
	img1.setImageResource( R.drawable.pig2 );
	
	pigStartToFire();
}

public void drawOnLayout(int id,int xPos,int yPos){
	
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1920/10,1028/5);
		img1 = new ImageView(workingActivity);
		img1.setImageResource( R.drawable.pig );
		img1.setX(xPos);
		img1.setY(yPos);
		img1.setId(id);
		pigId = id-21;
		
		workingLayout.addView(img1, params);
	
		pigStartToDropCoin();
}

public Enemy[] getEnemyArray(){
	Enemy[] tmp =  workingActivity.getEnemyArray();
	
	return tmp;
	
	
}

public void pigStartToFire(){
	
	pigTimer2 = new Timer();	

	pigTimer2.scheduleAtFixedRate(new TimerTask() {

		public void run() {
            workingActivity.runOnUiThread(new Runnable() //run on ui thread
             {

              public void run() 
              {
            	  
            	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1920/10,1028/5);
          		final ImageView fire  = new ImageView(workingActivity);
          		fire.setImageResource( R.drawable.fire );
          		fire.setX(img1.getX());
          		fire.setY(img1.getY()+30);
  
          		Animation a = AnimationUtils.loadAnimation(workingActivity.getApplicationContext(), R.anim.rotate);
          		fire.startAnimation(a);
          		
          		//Animation rotation = 

          		
          		workingLayout.addView(fire, params);
          		TranslateAnimation tAnimation ;

          		
          		if(pigId<4){
          			
          		
          		 tAnimation = new TranslateAnimation(0,0,0,150); 
          		 tAnimation.setDuration(600);
          		Matrix matrix=new Matrix();
         		 fire.setScaleType(ScaleType.MATRIX);   //required
         		 matrix.postRotate(0,100,100);
         		 fire.setImageMatrix(matrix);
          		 }else{
          			
          			 tAnimation = new TranslateAnimation(0,0,0,-150); 
              		 tAnimation.setDuration(600);
              		 Matrix matrix=new Matrix();
              		 fire.setScaleType(ScaleType.MATRIX);   //required
              		 matrix.postRotate((float)-180,100,100);
              		 fire.setImageMatrix(matrix);
              		
          		}
    	       
    	        tAnimation.setFillAfter(false);
    	        //tAnimation.set
    	       //tAnimation.setAnimationListener();
    	        //tAnimation.setRepeatCount(0);
    	       // tAnimation.setRepeatMode(Animation.REVERSE);
    	        fire.setAnimation(tAnimation);
    	        tAnimation.setAnimationListener(new AnimationListener() {
				
					
					@Override
					public void onAnimationEnd(Animation animation) {
						
						Boolean ifHit = false;
						
						// TODO Auto-generated method stub
						Enemy[] tmp = getEnemyArray();
						
						for(int i = 0; i< 10 ; i++){
					   		
					   		if(tmp[i]!=null){
					   			
					   			Rect myViewRect = new Rect();
						   		fire.getHitRect(myViewRect);
						   		
						   		float xHole = fire.getX();
						   		float xBall = tmp[i].getXEnemy();
						   		
						   		float yHole = fire.getY();
						   		float yBall =  tmp[i].getYEnemy();
						   		System.out.println("X fire: " + xHole);
								System.out.println("Y fire: " + yHole);
								System.out.println("X He: " + xBall);
								System.out.println("Y He: " + yBall);
								if (xHole - xBall <= 100.0 && xHole - xBall > -20.0) {
									//if (yHole - yBall < 55.0 && yHole - yBall > -55.0) {
										System.out.println("Mooo");
										//workingActivity.removeEnemy(tmp[i]);
										workingActivity.removeEnemy(tmp[i]);			
										
									//}
								}
//
					   		//Rect otherViewRect1 = tmp[i].getEnemyView();

					   		
					   		

					   		//if(true) {
					   		//	System.out.println("Mooo");
					   			
					   			//ifHit = true;
					   			//workingActivity.setText("Hit Human");
					   			//workingActivity.removeEnemy(tmp[i]);
					   			//workingLayout.removeView(tmp[i].get);
					   			//workingActivity.removeEnemy(tmp[i]);
					   		 // workingLayout.removeView(fire); 
					   			//tmp[i].goodbye();
					   			
					   		//}
					   		
					   		
						}
						
					   		workingLayout.removeView(fire); 
							
						 
						}
					}

					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub
						
					}
				});
    	        
    		    fire.startAnimation(tAnimation);
    		    
    		   // Animation animation = AnimationUtils.loadAnimation(parentView.getContext(),0);
            	  
            	//eatGrass();
              }
             });
        }
    
		},1000,1500);}
	
}
